package com.wawayaya.ui.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import com.wawayaya.bean.HomePage;
import com.wawayaya.util.Constants;

import java.util.HashMap;
import java.util.List;
import wawayaya.kids_iread.R;

/**
 *  Book shelf  continer
 */
public class BookContiner extends LinearLayout {

	private Context       context;
    private BookRack[]    bookRacks = null;
    private AdvertRack[]  advertRacks = null;

    private boolean isZh= false;

    private final HashMap<String, Integer> catalogIcons = new HashMap<String, Integer>();
    private final HashMap<String, Integer> moreIcons = new HashMap<String, Integer>();

	public BookContiner(Context context) {
		super(context);
        this.context = context;
	}
	
	public BookContiner(Context context, AttributeSet attrs) {
		super(context, attrs);
        this.context = context;
	}

	public BookContiner(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
        this.context = context;
	}

    /**
     * 初始化view data
     */
    public void initData(boolean isZh){
        this.isZh = isZh;
    }

    /**
     *  show view
     */
    public void showView(List<HomePage> homePageList){
        this.setCatalogColor();
        this.setMoreColor();
        if(null == homePageList) {
            return;
        }
        if(homePageList.size() <= 0) {
            return;
        }
        if(null != bookRacks) {
            this.removeAllViews();
            bookRacks = null;
        }
        if(null != advertRacks) {
            this.removeAllViews();
            advertRacks = null;
        }

        int  bookRackCount=  homePageList.size();
        bookRacks = new BookRack[bookRackCount];
        advertRacks = new AdvertRack[bookRackCount];

        for(int i=0;i<homePageList.size();i++){
            final HomePage  homePage = homePageList.get(i);
            if(homePage.getSectionType().intValue() == 1 || (homePage.getSectionType()==2 &&
                    homePage.getSectionCode().equals(Constants.common_bs) || homePage.getSectionCode().equals(Constants.prom_bs))  ){
                View v = LayoutInflater.from(context).inflate(R.layout.book_rack, null);
                bookRacks[i] = new BookRack(context, v, homePage,isZh, catalogIcons,moreIcons);
                this.addView(bookRacks[i].getView());
            }else if((homePage.getSectionType()==2 &&
                    homePage.getSectionCode().equals(Constants.adv_bs))){
                View v = LayoutInflater.from(context).inflate(R.layout.advert_rack, null);
                advertRacks[i] = new AdvertRack(context,v,homePage);
                this.addView(advertRacks[i].getView());
            }
        }
    }

    private void setCatalogColor(){
        catalogIcons.put("#868EC6", R.drawable.icon_flower_purple);
        catalogIcons.put("#FF9C01", R.drawable.icon_flower_yellow);
        catalogIcons.put("#629964", R.drawable.icon_flower_red);
        catalogIcons.put("#64A2EC", R.drawable.icon_flower_blue);
        catalogIcons.put("#899ea9", R.drawable.icon_flower_green);
        catalogIcons.put("#8b8dcb", R.drawable.icon_flower_faint_yellow);
    }

    private void setMoreColor(){
        moreIcons.put("#868EC6", R.drawable.more_arrow_purple);
        moreIcons.put("#FF9C01", R.drawable.more_arrow_yellow);
        moreIcons.put("#629964", R.drawable.more_arrow_red);
        moreIcons.put("#64A2EC", R.drawable.more_arrow_blue);
        moreIcons.put("#899ea9", R.drawable.more_arrow_green);
        moreIcons.put("#8b8dcb", R.drawable.more_arrow_faint_yellow);
    }

}
